package com.easepal802s.project.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.ble.HexUtils;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.view.LongClickImageView;
import com.ogawa.base.utils.CommonUtil;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.easepal802s.project.ble.BleController.CONNECT_BREAK;

/**
 * Created by FHB on 2018/4/24.
 */
public class MusicActivity extends Activity implements BleDataObserver.OnBleListerner,View.OnClickListener {
    private ImageView mIvClose;
    private List<ImageView> mImageList = new ArrayList<>();
    private LongClickImageView mIvAdd;
    private LongClickImageView mIvSub;
    private byte mData;
    private int mPosition = 0;
    private MyHandler myHandler;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_dialog);
        intView();
        BleDataObserver.getInst().registerBleDataObserver(this);
    }

    private void intView(){
        mImageList.clear();
        myHandler = new MyHandler(this);
        mIvClose = findViewById(R.id.music_iv_close);
        mIvAdd = findViewById(R.id.music_add);
        mIvAdd.intiListener();
        mIvSub = findViewById(R.id.music_sub);
        mIvSub.intiListener();
        mImageList.add((ImageView) findViewById(R.id.iv_music0));
        mImageList.add((ImageView) findViewById(R.id.iv_music1));
        mImageList.add((ImageView) findViewById(R.id.iv_music2));
        mImageList.add((ImageView) findViewById(R.id.iv_music3));
        mImageList.add((ImageView) findViewById(R.id.iv_music4));
        mImageList.add((ImageView) findViewById(R.id.iv_music5));
        mIvSub.setOnClickListener(this);
        mIvSub.intiListener();
        mIvAdd.setOnClickListener(this);
        mIvAdd.intiListener();
        mIvClose.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_in_center, R.anim.fade_out_center);
    }

    public void showViewPosition(int position) {
        Log.e("getMusicPosition",position+"");
        for(int i = 0;i<mImageList.size();i++){
            if(i==position){
                mImageList.get(i).setVisibility(View.VISIBLE);
            }
            else {
                mImageList.get(i).setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.music_sub:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.YINYSUB,true);
                }

                break;
            case R.id.music_add:
                if (CommonUtil.isFastClick()) {
                    return;
                } else {
                    BleController.getInst().sendSingleData(BleConstant.YINYADD,true);
                }

                break;
            case R.id.music_iv_close:
                finish();
                break;
        }

    }

    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
          myHandler.obtainMessage(1,data).sendToTarget();
    }
    private void getDatas(String data){
        if (data.startsWith(BleConstant.BAG2_HEAD)) {
            byte[] buffer = HexUtils.hexToByte(data);
            byte byte11 = buffer[11];
            if(mData!=byte11){
                mData = byte11;

                 if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,4))){
                    mPosition=5;
                }
                 else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,3))){
                     mPosition=4;
                 }
                 else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,2))){
                     mPosition=3;
                 }
                 else if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,1))){
                     mPosition=2;
                 }
                 else  if(CommonUtil.isOpera(HexUtils.getByteInPosition(data,11,0))){
                    mPosition=1;
                }

                else {
                    mPosition=0;
                }
                showViewPosition(mPosition);

            }



        }
    }

    @Override
    public void onPowerOff() {

        if(BleController.getInst().getConnectState()==CONNECT_BREAK){
            ActivityUtils.startHomeActivity(this,"BREAK");
        }
        else {
            ActivityUtils.startHomeActivity(this,"none");
        }
        this.finish();

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air,String name,String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {
        if(CommonUtil.isActivityTop(this.getClass(),this)&&data==1){
            ActivityUtils.startShouldersAdjustmentActivity(this);
        }
    }

    private  class MyHandler extends Handler {
        WeakReference<MusicActivity> mMainActivityWeakReference;

        MyHandler(MusicActivity mMusicActivity) {
            mMainActivityWeakReference = new WeakReference<>(mMusicActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final MusicActivity mMusicActivity = mMainActivityWeakReference.get();
            if (mMusicActivity != null) {
                String data = (String) msg.obj;
                  getDatas(data);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleDataObserver.getInst().removeBleDataObserver(this);
    }
}
